package com.ray.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.ray.bean.AdvancePurchaseBean;
import com.ray.bean.CompanyMaterialTableBean;
import com.ray.bean.CustomerBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.OrderInfoDetailBean;
import com.ray.exception.ServiceException;
import com.ray.mapper.AdvancePurchaseMapepr;
import com.ray.mapper.ApprovalMapper;
import com.ray.mapper.BasicDataMapper;
import com.ray.mapper.DingTalkMapper;
import com.ray.util.StringUtil;

/**
 * 处理预采购的Service
* <p>Title: AdvancePurchaseService</p>  
* <p>Description: </p>  
* @author 李斌  
* @date 2018年8月23日
 */
@Service
public class AdvancePurchaseService {
	
	@Autowired
	AdvancePurchaseMapepr advancePurchaseMapper;
	@Autowired
	DingTalkMapper dingTalkMapper;
	@Autowired
	BasicDataMapper basicDataMapper;
	@Autowired
	ApprovalMapper approvalMapper;
	
	public void addAdvancePurchase(Map<String, String> map) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		AdvancePurchaseBean advancePurchaseBean = new AdvancePurchaseBean();
		String advancePurchaseId = dingTalkMapper.getApprovalNo("YCG");
		advancePurchaseBean.setMainId(advancePurchaseId);
		advancePurchaseBean.setDetailId(StringUtil.getUUID());
		advancePurchaseBean.setPurchaseOrderId(StringUtil.getUUID());
		advancePurchaseBean.setArticleNo(map.get("articleNo"));
		advancePurchaseBean.setOrderId(map.get("orderId")); 
		advancePurchaseBean.setCompanyOrderId(map.get("companyOrderId"));
		advancePurchaseBean.setCurrency("人民币");
		advancePurchaseBean.setType(1); 
		advancePurchaseBean.setStatus(0);
		advancePurchaseBean.setMaterialName(map.get("materialName"));
		advancePurchaseBean.setModel(map.get("model"));
		advancePurchaseBean.setColor(map.get("color"));
		advancePurchaseBean.setColorDescription(map.get("colorDescription"));
		advancePurchaseBean.setProviderName(map.get("providerName"));
		advancePurchaseBean.setCreateUser(map.get("loginUserName"));
		advancePurchaseBean.setCreateUserId(map.get("loginUserId"));
		advancePurchaseBean.setPurchasePeople(map.get("loginUserName"));
		advancePurchaseBean.setDefaultWarehouse(map.get("defaultWarehouse"));
		advancePurchaseBean.setFactoryNo(map.get("factoryNo"));
		advancePurchaseBean.setCreateTime(simpleDateFormat.format(new Date()));
		advancePurchaseBean.setTax(16.0);
		advancePurchaseBean.setUnit(map.get("unit"));
		advancePurchaseBean.setPrice(Double.valueOf(map.get("price")));
		advancePurchaseBean.setMaterialType(map.get("materialType"));
		advancePurchaseBean.setPurchaseCount(Double.valueOf(map.get("purchaseCount")));
		advancePurchaseBean.setMaterial(map.get("material"));
		advancePurchaseBean.setCompany(map.get("loginCompany"));
		advancePurchaseBean.setProcurementCycles(Integer.valueOf(map.get("procurementCycles")));
		if(map.get("transmittance")!=null) { 
			advancePurchaseBean.setTardiness(Integer.valueOf(map.get("transmittance")));
		}
		 
		//老对象
		String orderInfoDetail = map.get("oldOrderInfoDetail").toString();
		OrderInfoDetailBean oldorderInfoDetail = JSONObject.parseObject(orderInfoDetail, OrderInfoDetailBean.class);
		advancePurchaseBean.setOldColor(oldorderInfoDetail.getColor());
		advancePurchaseBean.setOldModel(oldorderInfoDetail.getMaterialModel());
		advancePurchaseBean.setOldColorDescription(oldorderInfoDetail.getColorDescription());
		advancePurchaseBean.setOldMaterialFactory(oldorderInfoDetail.getMaterialFactory());
		List<AdvancePurchaseBean> advancePurchaseBeans = getAdvancePurchaseByOrderDetail(advancePurchaseBean);
		if(advancePurchaseBeans !=null && advancePurchaseBeans.size()>0) {
			throw new ServiceException("该订单已生成预采购单,请勿重复生成");
		} 
		int row = advancePurchaseMapper.addAdvancePurchase(advancePurchaseBean);
		if(row<1) {
			throw new ServiceException("新增预采购单失败");
		}
	}
	
	/**
	 * 分页查询预采购单
	* @date 2018年8月24日 下午1:44:55 
	* @author 李斌
	* @return DatatablesViewPageBean<AdvancePurchaseBean> 
	* @throws
	 */
	public DatatablesViewPageBean<AdvancePurchaseBean> getAdvancePurchase(Map<String, Object> map) {
		DatatablesViewPageBean<AdvancePurchaseBean> datatablesViewPageBean = new DatatablesViewPageBean<AdvancePurchaseBean>();
		List<AdvancePurchaseBean> advancePurchaseBeans = advancePurchaseMapper.getAdvancePurchase(map);
		List<CustomerBean> customerBeans = basicDataMapper.queryCustomers(map);
		Map<String,Object> approvalParam = new HashMap<String,Object>();
		approvalParam.put("type","4"); 
		approvalParam.put("id",map.get("loginUserId"));  
		approvalParam.put("loginCompany", map.get("loginCompany"));
		//获取当前登录人可以审核的人员
		List<String> userIds = approvalMapper.getHaveId(approvalParam);
		for(AdvancePurchaseBean pojo:advancePurchaseBeans) {
			for(CustomerBean customerBean:customerBeans) {
				if(pojo.getProviderName().equals(customerBean.getCustomerName())) {
					pojo.setCustomerBean(customerBean);
					break;
				}
			} 
			for(String userId:userIds) {
				if(pojo.getCreateUserId().equals(userId)) {
					pojo.setIsApprovalUser(1);
					break;
				}
			}
		} 
		int count = advancePurchaseMapper.getAdvancePurchaseCount(map);
		datatablesViewPageBean.setiTotalDisplayRecords(count);
		datatablesViewPageBean.setiTotalRecords(count);
		datatablesViewPageBean.setReturnData(advancePurchaseBeans);
		return datatablesViewPageBean;
	}
	
	/**
	 * 查重
	* @date 2018年8月24日 上午11:11:24 
	* @author 李斌
	* @return List<AdvancePurchaseBean> 
	* @throws
	 */
	public List<AdvancePurchaseBean> getAdvancePurchaseByOrderDetail(AdvancePurchaseBean advancePurchaseBean){
		List<AdvancePurchaseBean> advancePurchaseBeans = advancePurchaseMapper.getAdvancePurchaseByOrderDetail(advancePurchaseBean);
		return advancePurchaseBeans;
	}
	
	/**
	 * 设置复期
	* @date 2018年8月24日 下午2:03:38 
	* @author 李斌
	* @return void 
	* @throws
	 */ 
	public void setLaterPhaseData(Map<String, Object> map) {
		advancePurchaseMapper.setLaterPhaseData(map);
	}

	public List<AdvancePurchaseBean> getAdvancePurchaseByPrint(Map<String, Object> map) {
		if(map.get("ids") ==null) {
			throw new ServiceException("参数异常 ids 为空");
		}
		map.put("ids", map.get("ids").toString().split(","));
		List<AdvancePurchaseBean> advancePurchaseBeans = advancePurchaseMapper.getAdvancePurchase(map);
		List<CustomerBean> customerBeans = basicDataMapper.queryCustomers(map);
		for(AdvancePurchaseBean pojo:advancePurchaseBeans) {
			for(CustomerBean customerBean:customerBeans) {
				if(pojo.getProviderName().equals(customerBean.getCustomerName())) {
					pojo.setCustomerBean(customerBean);
					break;
				}
			} 
		}  
		return advancePurchaseBeans;
	}
	
	/**
	 * 查预采购单信息
	* @date 2018年8月25日 上午8:07:00 
	* @author 李斌
	* @return AdvancePurchaseBean 
	* @throws
	 */
	public AdvancePurchaseBean getAdvancePurchaseByDetail(Map<String, String> map) {
		AdvancePurchaseBean advancePurchaseBean = new AdvancePurchaseBean();
		advancePurchaseBean.setOrderId(map.get("orderId"));
		advancePurchaseBean.setOldColor(map.get("oldColor"));
		advancePurchaseBean.setMaterialType(map.get("materialType"));
		advancePurchaseBean.setOldColorDescription(map.get("oldColorDescription"));
		advancePurchaseBean.setOldModel(map.get("oldModel"));
		advancePurchaseBean.setOldMaterialFactory(map.get("oldMaterialFactory"));
		advancePurchaseBean.setCompany(map.get("loginCompany"));
		List<AdvancePurchaseBean> advancePurchaseBeans = this.getAdvancePurchaseByOrderDetail(advancePurchaseBean);
		if( advancePurchaseBeans.size()>1) {
			throw new ServiceException("多条重复预采购单异常");
		}
		if(advancePurchaseBeans!=null && advancePurchaseBeans.size()==1) {
			return advancePurchaseBeans.get(0);
		}
		return null;
	}
	
	/**
	 * 删除预采购单通过mainId
	* @date 2018年8月25日 上午11:17:09 
	* @author 李斌
	* @return void 
	* @throws
	 */ 
	public void deleteAdvancePurchaseByMainId(Map<String, Object> map) {
		int row = advancePurchaseMapper.deleteAdvancePurchaseByMainId(map);
		if(row<1) {
			throw new ServiceException("删除预采购单失败");
		}
	}

	public void agreeAdvancePurchaseByMainId(Map<String, Object> map) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("approvalTime", simpleDateFormat.format(new Date()));
		int row = advancePurchaseMapper.agreeAdvancePurchaseByMainId(map);
		if(row<1) { 
			throw new ServiceException("审核预采购单失败");
		}
	}
	
	/**
	 * 修改预采购单物料信息
	* @date 2018年9月3日 上午8:18:03 
	* @author 李斌
	* @return void 
	* @throws
	 */
	public void updateAdvancePurchaseByMainId(Map<String, Object> map) {
		Map<String,Object> approvalParam = new HashMap<String,Object>();
		approvalParam.put("type","4"); 
		approvalParam.put("id",map.get("loginUserId"));  
		approvalParam.put("loginCompany", map.get("loginCompany"));
		//获取当前登录人可以审核的人员
		List<String> userIds = approvalMapper.getHaveId(approvalParam);
		AdvancePurchaseBean advancePurchaseBean = advancePurchaseMapper.getAdvancePurchaseByMainId(map);
		boolean isApproval = false;
		for(String userId:userIds) {
			if(advancePurchaseBean.getCreateUserId().equals(userId)) {
				isApproval = true;
				break;
			}
		} 
		if(!isApproval) {
			throw new ServiceException("无审批权限,不能修改");
		}
		Map<String,Object> materialUnitMap = new HashMap<String,Object>();
		materialUnitMap.put("materialName", map.get("materialName"));
		materialUnitMap.put("model",map.get("model"));
		materialUnitMap.put("providerName",map.get("providerName"));
		if(map.get("color")==null) {
			materialUnitMap.put("color", "");
		}else {
			materialUnitMap.put("color", map.get("color"));
		}
		materialUnitMap.put("loginCompany",map.get("loginCompany"));
		List<CompanyMaterialTableBean> companyMaterialTableBeans = basicDataMapper.getCompanyMaterialByDetail(materialUnitMap);
		if(companyMaterialTableBeans !=null && companyMaterialTableBeans.size()>0) {
			throw new ServiceException("该物料已存在，请再次确认");
		} 
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		map.put("updateMateiralTime", simpleDateFormat.format(new Date()));
		map.put("updateMaterialUser", map.get("loginUserName"));
		int row  = advancePurchaseMapper.updateAdvancePurchaseByMainId(map);
		if(row<1) { 
			throw new ServiceException("修改预采购单物料信息失败");
		}
	}
	
	/**
	 * 获取预采购单数据(用于采购单入库)
	* @date 2018年9月8日 下午4:18:32 
	* @author 李斌
	* @return List<CompanyMaterialTableBean> 
	* @throws
	 */
	public List<CompanyMaterialTableBean> getAdvancePurchaseData(Map<String, Object> map){
		return advancePurchaseMapper.getAdvancePurchaseData(map);
	}
}
